package com.blb.zy_community.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysArea;
import com.blb.zy_community.entity.SysRole;
import com.blb.zy_community.entity.ZyCommunity;
import com.blb.zy_community.service.ISysAreaService;
import com.blb.zy_community.service.IZyCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 小区  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class ZyCommunityController {
    @Autowired
    private IZyCommunityService zyCommunityService;

    /**
     * 小区信息的分页
     * @param current
     * @param size
     * @param communityName
     * @param communityCode
     * @return
     */
    @GetMapping("zyCommunity-page")
    public ResponseResult<Page<ZyCommunity>> getZyCommunityPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                                @RequestParam(required = false,defaultValue = "5") Long size,
                                                                @RequestParam(required = false) String communityName,
                                                                @RequestParam(required = false) String communityCode ,
                                                                @RequestParam(required = false) String communityId){
        Page<ZyCommunity> page = zyCommunityService.selectZyCommunityPage(new Page<>(current, size), communityName, communityCode , communityId);
        return ResponseResult.ok(page);
    }

    /**
     * 小区
     */
    @DeleteMapping("zyCommunity/{id}")
    public ResponseResult<String> deleteZyCommunity(@PathVariable Long id){
        zyCommunityService.removeById(id);
        return ResponseResult.ok(id);
    }

    /**
     * 添加
     */
    @PostMapping("zyCommunity")
    public ResponseResult<String> addZyCommunity(@RequestBody Map<String,Object> param){
        int communityProvenceCode = 0;
        int communityCityCode = 0;
        int communityTownCode = 0;
        Object zyCommunity = param.get("zyCommunity");
        Object areaList = param.get("areaList");
        ZyCommunity community = JSON.parseObject(JSON.toJSONString(zyCommunity), new TypeReference<ZyCommunity>() {
        });
        List<Long> areas = JSON.parseObject(JSON.toJSONString(areaList), new TypeReference<List<Long>>() {
        });
        for (int i = 0; i < areas.size(); i++) {
            communityProvenceCode = Math.toIntExact(areas.get(0));
            communityCityCode = Math.toIntExact(areas.get(1));
            communityTownCode = Math.toIntExact(areas.get(2));
        }
        community.setCommunityProvenceCode(String.valueOf(communityProvenceCode));
        community.setCommunityCityCode(String.valueOf(communityCityCode));
        community.setCommunityTownCode(String.valueOf(communityTownCode));
        community.setCreateTime(LocalDateTime.now());
        String s = "COMMUNITY_";
        String str = String.valueOf(new Long((long) ((Math.random() + 1) * Math.pow(10, 13 - 1))).longValue());
        String code = s + str;
        community.setCommunityCode(code);
        zyCommunityService.save(community);
        return ResponseResult.ok(param);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/communities/{communityId}")
    public ResponseResult<String> deleteCommunity(@PathVariable Long[] communityId){
        zyCommunityService.deleteCommunities(communityId);
        return ResponseResult.ok(communityId);
    }

    /**
     * 修改
     */
    @PutMapping("zyCommunity")
    public ResponseResult<String> updateZyCommunity(@RequestBody Map<String,Object> param){
        int communityProvenceCode = 0;
        int communityCityCode = 0;
        int communityTownCode = 0;
        Object zyCommunity = param.get("zyCommunity");
        Object areaList = param.get("areaList");
        ZyCommunity community = JSON.parseObject(JSON.toJSONString(zyCommunity), new TypeReference<ZyCommunity>() {
        });
        List<Long> areas = JSON.parseObject(JSON.toJSONString(areaList), new TypeReference<List<Long>>() {
        });
        for (int i = 0; i < areas.size(); i++) {
            communityProvenceCode = Math.toIntExact(areas.get(0));
            communityCityCode = Math.toIntExact(areas.get(1));
            communityTownCode = Math.toIntExact(areas.get(2));
        }
        community.setCommunityProvenceCode(String.valueOf(communityProvenceCode));
        community.setCommunityCityCode(String.valueOf(communityCityCode));
        community.setCommunityTownCode(String.valueOf(communityTownCode));
        community.setCreateTime(LocalDateTime.now());
        zyCommunityService.updateById(community);
        return ResponseResult.ok(param);
    }


    /**
     * 根据id查询单个信息
     */
    @RequestMapping("community/{id}")
    public ResponseResult<String> getCommunityOne(@PathVariable Long id){
        ZyCommunity zyCommunity = zyCommunityService.getOne(new QueryWrapper<ZyCommunity>().eq("community_id", id));
        return ResponseResult.ok(zyCommunity);
    }


    /**
     * 更换物业
     */
    @PutMapping("updateCommunity")
    public ResponseResult<String> updateCommunity(@RequestBody Map<String,Object> param){
        Object publicCommunityId = param.get("publicCommunityId");
        Object deptId = param.get("deptId");
        Object community = param.get("community");
        ZyCommunity zyCommunity = JSON.parseObject(JSON.toJSONString(community), new TypeReference<ZyCommunity>() {
        });
        zyCommunity.setCommunityId(Long.parseLong((String) publicCommunityId));
        zyCommunity.setDeptId(Long.parseLong(deptId.toString()));

        zyCommunityService.updateById(zyCommunity);
        return ResponseResult.ok(zyCommunity);
    }

    /**
     * 获取全部小区（集合）
     * @return
     */
    @GetMapping("/zyCommunity/list")
    public ResponseResult<List<ZyCommunity>> getCommunityList(){
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }


    /**
     * 查询所有小区
     */
    @RequestMapping("communities")
    public ResponseResult<String> communities(){
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }

}
